package AFMAnalysis;


import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;

public class AFMAnalysis extends JFrame{

	private JFrame mainFrame = null;  //  @jve:decl-index=0:visual-constraint="16,-13"
	private JPanel mainPane = null;
	private JCheckBox perpCheckBox = null;
	private JCheckBox parCheckBox = null;
	private JTextField currentImageText = null;
	private JButton currentImageButton = null;
	private JLabel currentImageLabel = null;
	private JLabel finalImageLocationLabel = null;
	private JButton finalImageLocationBrowse = null;
	private JTextField finalImageLocationText = null;
	private JLabel finalImageNameLabel = null;
	private JTextField finalImageNameText = null;
	private JButton runButton = null;
	private Control control = null;
	private File startingImage = null;
	private File finalImageLocation = null;
	private String finalImageName = null;
	private boolean perp = false;
	private boolean par = false;
	private JLabel resultsLabel = null;
	private JTextField perpPercentOutput = null;
	private JLabel perpPercentLabel = null;
	private JTextField parPercentOutput = null;
	private JLabel parPercentLabel = null;

	/**
	 * This method initializes mainFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	private JFrame getMainFrame() {
		if (mainFrame == null) {
			mainFrame = new JFrame();
			mainFrame.setSize(new Dimension(330, 513));
			mainFrame.setTitle("AFMAnalysis");
			mainFrame.setContentPane(getMainPane());
		}
		return mainFrame;
	}

	/**
	 * This method initializes mainPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMainPane() {
		if (mainPane == null) {
			parPercentLabel = new JLabel();
			parPercentLabel.setBounds(new Rectangle(178, 377, 96, 25));
			parPercentLabel.setText("Percent Parallel");
			perpPercentLabel = new JLabel();
			perpPercentLabel.setBounds(new Rectangle(14, 377, 131, 25));
			perpPercentLabel.setText("Percent Perpendicular");
			resultsLabel = new JLabel();
			resultsLabel.setBounds(new Rectangle(133, 337, 47, 20));
			resultsLabel.setText("Results");
			finalImageNameLabel = new JLabel();
			finalImageNameLabel.setBounds(new Rectangle(106, 258, 102, 26));
			finalImageNameLabel.setText("Final Image Name");
			finalImageLocationLabel = new JLabel();
			finalImageLocationLabel.setBounds(new Rectangle(95, 171, 117, 26));
			finalImageLocationLabel.setText("Final Image Location");
			currentImageLabel = new JLabel();
			currentImageLabel.setBounds(new Rectangle(56, 73, 196, 28));
			currentImageLabel.setText("Starting Image Name and Location");
			mainPane = new JPanel();
			mainPane.setLayout(null);
			mainPane.add(getPerpCheckBox(), null);
			mainPane.add(getParCheckBox(), null);
			mainPane.add(getCurrentImageText(), null);
			mainPane.add(getCurrentImageButton(), null);
			mainPane.add(currentImageLabel, null);
			mainPane.add(finalImageLocationLabel, null);
			mainPane.add(getFinalImageLocationBrowse(), null);
			mainPane.add(getFinalImageLocationText(), null);
			mainPane.add(finalImageNameLabel, null);
			mainPane.add(getFinalImageNameText(), null);
			mainPane.add(getRunButton(), null);
			mainPane.add(resultsLabel, null);
			mainPane.add(getPerpPercentOutput(), null);
			mainPane.add(perpPercentLabel, null);
			mainPane.add(getParPercentOutput(), null);
			mainPane.add(parPercentLabel, null);
		}
		return mainPane;
	}

	/**
	 * This method initializes perpCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getPerpCheckBox() {
		if (perpCheckBox == null) {
			perpCheckBox = new JCheckBox();
			perpCheckBox.setBounds(new Rectangle(15, 36, 107, 22));
			perpCheckBox.setText("Perpendicular");
		}
		return perpCheckBox;
	}

	/**
	 * This method initializes parCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getParCheckBox() {
		if (parCheckBox == null) {
			parCheckBox = new JCheckBox();
			parCheckBox.setBounds(new Rectangle(149, 36, 75, 22));
			parCheckBox.setText("Parallel");
		}
		return parCheckBox;
	}

	/**
	 * This method initializes currentImageText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCurrentImageText() {
		if (currentImageText == null) {
			currentImageText = new JTextField();
			currentImageText.setBounds(new Rectangle(13, 126, 185, 24));
			currentImageText.setText("Input required here");
		}
		return currentImageText;
	}

	/**
	 * This method initializes currentImageButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCurrentImageButton() {
		if (currentImageButton == null) {
			currentImageButton = new JButton();
			currentImageButton.setBounds(new Rectangle(216, 123, 82, 32));
			currentImageButton.setText("Browse");
			currentImageButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fileChooser.setDialogTitle("Starting Image Name and Location");
					fileChooser.showOpenDialog(mainFrame);
					startingImage = fileChooser.getSelectedFile();
					currentImageText.setText(startingImage.getPath());
				}
			});
		}
		return currentImageButton;
	}

	/**
	 * This method initializes finalImageLocationBrowse	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getFinalImageLocationBrowse() {
		if (finalImageLocationBrowse == null) {
			finalImageLocationBrowse = new JButton();
			finalImageLocationBrowse.setBounds(new Rectangle(216, 213, 82, 32));
			finalImageLocationBrowse.setText("Browse");
			finalImageLocationBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					fileChooser.setDialogTitle("Final Image Location");
					fileChooser.showOpenDialog(mainFrame);
					finalImageLocation = fileChooser.getSelectedFile();
					finalImageLocationText.setText(finalImageLocation.getPath());
				}
			});
		}
		return finalImageLocationBrowse;
	}

	/**
	 * This method initializes finalImageLocationText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFinalImageLocationText() {
		if (finalImageLocationText == null) {
			finalImageLocationText = new JTextField();
			finalImageLocationText.setBounds(new Rectangle(13, 215, 185, 24));
			finalImageLocationText.setText("Input required here");
		}
		return finalImageLocationText;
	}

	/**
	 * This method initializes finalImageNameText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFinalImageNameText() {
		if (finalImageNameText == null) {
			finalImageNameText = new JTextField();
			finalImageNameText.setBounds(new Rectangle(65, 298, 185, 24));
			finalImageNameText.setText("Input file name here");
		}
		return finalImageNameText;
	}

	/**
	 * This method initializes runButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRunButton() {
		if (runButton == null) {
			runButton = new JButton();
			runButton.setBounds(new Rectangle(240, 15, 59, 42));
			runButton.setText("Run!");
			runButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(perpCheckBox.isSelected()){
						perp = true;
					}
					else{
						perp = false;
					}
					if(parCheckBox.isSelected()){
						par = true;
					}
					else{
						par = false;
					}
					JFileChooser openChooser = new JFileChooser();
					openChooser.setDialogType(JFileChooser.OPEN_DIALOG);
					openChooser.showOpenDialog(mainPane);
					openChooser.setDialogTitle("Open File");
					startingImage = openChooser.getSelectedFile();
					JFileChooser saveChooser = new JFileChooser();
					saveChooser.setDialogType(JFileChooser.SAVE_DIALOG);
					saveChooser.showSaveDialog(mainPane);
					saveChooser.setDialogTitle("Save Location");
					finalImageLocation = saveChooser.getCurrentDirectory();
					finalImageName = saveChooser.getName(saveChooser.getSelectedFile());
					control = new Control(startingImage);
					control.createBlobs();
					control.blobArray = control.reduceBlobs(5);
					Solver solver = new Solver();
					//Make this false -> true to get the size distribution
					solver.solve(control.blobArray, finalImageLocation, finalImageName, false);
					control.generateFinalPicture(finalImageName, finalImageLocation, perp, par);
					GrainDetector g = new GrainDetector(control.pixels, control.blobArray);
					//g.generateFinalScaledPicture(finalImageName, finalImageLocation, perp, par);
					System.out.println("Percent Parallel:");
					double percentPar = control.roundTwoDecimals((double)solver.parallel*100/(solver.parallel+solver.perpendicular));
					System.out.println(percentPar);
					System.out.println("Percent Perpendicular:");
					double percentPerp = control.roundTwoDecimals((double)solver.perpendicular*100/(solver.parallel+solver.perpendicular));
					System.out.println(percentPerp);
					perpPercentOutput.setText(percentPerp + " ");
					parPercentOutput.setText(percentPar + " ");
					//Make this false -> true to get the pair distribution function
					control.getRadialDistribution(solver, finalImageLocation, finalImageName, false);
					System.out.println("Done!");

					System.out.println("\n\n\nAttempting to identify the perpedicular grains.");
					System.out.println("The average distance between closest centers is " + solver.criticalRValue);
					g.getAngleGrains(0.1, 3*solver.criticalRValue + 30);
					System.out.println("Grains identified. Drawing...");
					g.drawPerpendicularAngleGrains(finalImageLocation, finalImageName);
//					System.out.println(g.scaledBlobs.get(1).pixels.get(0).x + ", " + g.scaledBlobs.get(1).pixels.get(0).y);
//					System.out.println(g.centers.get(0)[0] + ", " + g.centers.get(0)[1]);
//					System.out.println(control.blobArray.get(0).head.x + ", " + control.blobArray.get(0).head.y);
					System.out.println("Finished.");
					//Comment the following out to run properly until this is finished

//					g.getEdges();//finalImageLocation, finalImageName);
//					g.edger.drawEdges(finalImageLocation, finalImageName);
//					g.createLines(0.45);
//					System.out.println("Number of initial Lines: " + g.lines.size());
//					g.reduceLines(25);
//					System.out.println("Number of final Lines: " + g.lines.size());
//					g.drawLines(finalImageLocation, finalImageName);
//					long start = System.currentTimeMillis();
//					g.createGrains();
//					long end = System.currentTimeMillis();
//					System.out.println("That took " + (double)(end-start)/60000 + " minutes.");
//					System.out.println(g.grains.size());
//					System.out.println("Reducing");
//					g.reduceGrains();
//					for(int count = 0; count < g.grains.size(); count++){
//						System.out.println(g.grains.get(count).scaledBlobs.size());
//						System.out.println(g.grains.get(count).scaledBlobs.get(0).pixels.get(0).x
//								+ ", " + g.grains.get(count).scaledBlobs.get(0).pixels.get(0).y);
//					}
//					System.out.println(g.grains.size());
//					g.drawGrains(finalImageLocation, finalImageName);
//					for(int count = 0; count < g.grains.size(); count++){
//						System.out.println(count + ", " + g.grains.get(count).scaledBlobs.size());
//					}
//					System.out.println(g.lines.get(0).slope);
//					System.out.println(g.lines.get(0).pixels.get(0).x + ", " + g.lines.get(0).pixels.get(0).y);
//					System.out.println(g.lines.get(1).slope);
//					System.out.println(g.lines.get(1).pixels.get(0).x + ", " + g.lines.get(1).pixels.get(0).y);
//					System.out.println(g.lines.get(2).slope);
//					System.out.println(g.lines.get(2).pixels.get(0).x + ", " + g.lines.get(2).pixels.get(0).y);
//					System.out.println(g.lines.get(3).slope);
//					System.out.println(g.lines.get(3).pixels.get(0).x + ", " + g.lines.get(3).pixels.get(0).y);
//					System.out.println(g.lines.get(4).slope);
//					System.out.println(g.lines.get(4).pixels.get(0).x + ", " + g.lines.get(4).pixels.get(0).y);
//					
				}
			});
		}
		return runButton;
	}

	/**
	 * This method initializes perpPercentOutput	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPerpPercentOutput() {
		if (perpPercentOutput == null) {
			perpPercentOutput = new JTextField();
			perpPercentOutput.setBounds(new Rectangle(44, 421, 82, 21));
			perpPercentOutput.setText("No Input Here");
		}
		return perpPercentOutput;
	}

	/**
	 * This method initializes parPercentOutput	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getParPercentOutput() {
		if (parPercentOutput == null) {
			parPercentOutput = new JTextField();
			parPercentOutput.setBounds(new Rectangle(184, 421, 82, 21));
			parPercentOutput.setText("No Input Here");
		}
		return parPercentOutput;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		AFMAnalysis main = new AFMAnalysis();
		JFrame frame = main.getMainFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}  //  @jve:decl-index=0:visual-constraint="355,-8"
